package com.bawei.mall.product.service.impl;

import com.bawei.mall.product.mapper.CBrandMapper;
import com.bawei.mall.product.service.ICBrandService;
import com.bawei.mall.product.domain.Po.Brand;
import com.bawei.mall.product.domain.Po.BrandThumbsUp;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Random;

/**
 * @Author：liurui
 * @Package：com.bawei.mall.search.service.impl
 * @Project：mall_cloud
 * @name：BrandServiceImpl
 */
@Service
@Log4j2
public class CBrandServiceImpl implements ICBrandService {
    @Autowired
    private CBrandMapper mapper;

    @Override
    public Brand detail(Integer id) {
        return mapper.detail(id);
    }

    @Override
    public void delete(Integer brandId) {
        mapper.delete(brandId);
    }

    @Override
    public BrandThumbsUp detailup(Integer brandId) {
        return mapper.detailup(brandId);
    }

    @Override
    public void deletezi(Integer productId) {
        mapper.deletezi(productId);
    }

    @Override
    public List<BrandThumbsUp> list() {
        return mapper.list();
    }

    @Override
    public void clear() {
        mapper.clear();
    }

    @Override
    public void add(BrandThumbsUp brandThumbsUp) {
        String generatedString = generateRandomString(16);
        try {
            brandThumbsUp.setId(generatedString);
            mapper.add(brandThumbsUp);
        } catch (Exception e) {
            // 捕获并处理异常，例如记录错误日志
            log.error("Failed to add BrandThumbsUp with ID {}: {}", generatedString, e.getMessage());
            // 根据需要抛出异常或进行其他错误处理
            throw new RuntimeException("Failed to add BrandThumbsUp", e);
        }

    }

    @Override
    public void addzi(BrandThumbsUp brandThumbsUp) {
        String generatedString = generateRandomString(16);
        try {
            brandThumbsUp.setId(generatedString);
            mapper.addzi(brandThumbsUp);
        } catch (Exception e) {
            // 捕获并处理异常，例如记录错误日志
            log.error("Failed to add BrandThumbsUp with ID {}: {}", generatedString, e.getMessage());
            // 根据需要抛出异常或进行其他错误处理
            throw new RuntimeException("Failed to add BrandThumbsUp", e);
        }
    }

    private String generateRandomString(int length) {
        String characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
        Random random = new Random();
        StringBuilder stringBuilder = new StringBuilder(length);
        for (int i = 0; i < length; i++) {
            int index = random.nextInt(characters.length());
            stringBuilder.append(characters.charAt(index));
        }
        return stringBuilder.toString();
    }


}
